package com.wiloon.java8x.thread.single;

import java.lang.management.ManagementFactory;
import java.util.Date;

/**
 * Created by roy.wang on 2017/3/30;
 */
public class SingleThreadX {
    public static void main(String[] args) {
        SingleThreadX singleThreadX = new SingleThreadX();
        singleThreadX.run0();
    }

    public void run() {
        Thread thread = new Thread(new ThreadX());
        thread.setName("test_thread");
        thread.start();
    }

    class ThreadX implements Runnable {
        @Override
        public void run() {
            while (true) {
                String name = ManagementFactory.getRuntimeMXBean().getName();
                String pid = name.split("@")[0];
                System.out.println(String.format("thread running, name:%s, pid:%s, timestamp:%s", name, pid, new Date()));
                try {
                    Thread.sleep(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void run0() {
        while (true) {
            String name = ManagementFactory.getRuntimeMXBean().getName();
            String pid = name.split("@")[0];
            System.out.println(String.format("thread running, name:%s, pid:%s, timestamp:%s", name, pid, new Date()));
            try {
                Thread.sleep(5);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
